#!/bin/bash
rm -rf dist
mkdir dist

workDir=$(pwd)
pkg="gofile"

function buildPkg() {
  arch=$1
  oos=$2
  echo "build $oos $arch"
  cd $workDir/src
  buildPkgName=${pkg}-${oos}-${arch}
  if [ "$arch" == "arm64" ];
  then
    GOARM=7 GOARCH=arm64 GOOS=$oos go build -ldflags '-w -s' -o $buildPkgName
  else
    GOARCH=$arch GOOS=$oos go build -ldflags '-w -s' -o $buildPkgName
  fi

  targetDir=$workDir/dist/$buildPkgName
  mkdir -p $targetDir/src/static/
  mv $buildPkgName $targetDir/gofile
  cp -rf ${workDir}/src/static/* $targetDir/src/static/
  cp -rf ${workDir}/scripts/* $targetDir/
  cp -rf ${workDir}/config.yaml $targetDir/
  cd $workDir/dist/
  mv $buildPkgName gofile
  gtar -zcvf ${buildPkgName}.tar.gz gofile
  echo "build success,dist files:${buildPkgName}.tar.gz"
  rm -rf $targetDir
  rm -rf $workDir/dist/gofile
}

buildPkg "amd64" "linux"
buildPkg "amd64" "darwin"
buildPkg "arm64" "linux"
